package com.ruoyi.elebook.controller;

import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.elebook.domain.BookMajor;
import com.ruoyi.elebook.service.IBookMajorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 专业Controller
 *
 * @author ruoyi
 * @date 2023-12-11
 */
@RestController
@RequestMapping("/major")
public class BookMajorController extends BaseController
{
    @Autowired
    private IBookMajorService bookMajorService;

    /**
     * 查询专业列表
     */
    @RequiresPermissions("elebook:major:list")
    @GetMapping("/list")
    public TableDataInfo list(BookMajor bookMajor)
    {
        startPage();
        List<BookMajor> list = bookMajorService.selectBookMajorList(bookMajor);
        return getDataTable(list);
    }

    /**
     * 查询所有专业
     */
    @RequiresPermissions("elebook:major:list")
    @GetMapping("/alllist")
    public AjaxResult allList()
    {
        List<BookMajor> list = bookMajorService.selectBookMajorList(new BookMajor());
        return success(list);
    }

    /**
     * 导出专业列表
     */
    @RequiresPermissions("elebook:major:export")
    @Log(title = "专业", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BookMajor bookMajor)
    {
        List<BookMajor> list = bookMajorService.selectBookMajorList(bookMajor);
        ExcelUtil<BookMajor> util = new ExcelUtil<BookMajor>(BookMajor.class);
        util.exportExcel(response, list, "专业数据");
    }

    /**
     * 获取专业详细信息
     */
    @RequiresPermissions("elebook:major:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(bookMajorService.selectBookMajorById(id));
    }

    /**
     * 新增专业
     */
    @RequiresPermissions("elebook:major:add")
    @Log(title = "专业", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BookMajor bookMajor)
    {
        return toAjax(bookMajorService.insertBookMajor(bookMajor));
    }

    /**
     * 修改专业
     */
    @RequiresPermissions("elebook:major:edit")
    @Log(title = "专业", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BookMajor bookMajor)
    {
        return toAjax(bookMajorService.updateBookMajor(bookMajor));
    }

    /**
     * 删除专业
     */
    @RequiresPermissions("elebook:major:remove")
    @Log(title = "专业", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(bookMajorService.deleteBookMajorByIds(ids));
    }
}
